<?php
global $_GPC, $_W;
$sql="select * from".tablename('zhtc_account'). " where uid={$_W['user']['uid']} and find_in_set('hydel',city_qx)";
$account=pdo_fetch($sql);
$action = 'start';
$GLOBALS['frames'] = $this->getNaveMenu($_COOKIE['cityname'], $action);
$where=" where a.uniacid=:uniacid and a.cityname=:cityname ";
$type=isset($_GPC['type'])?$_GPC['type']:'wait';
if(isset($_GPC['keywords'])){
    $where.=" and a.company_name LIKE  concat('%', :name,'%') ";
    $data[':name']=$_GPC['keywords']; 
    $type='all';  
}
if(!empty($_GPC['time'])){
   $start=strtotime($_GPC['time']['start']);
   $end=strtotime($_GPC['time']['end']);
  $where.=" and a.sh_time >={$start} and a.sh_time<={$end} ";

}
if(!empty($_GPC['state'])){

  $where.=" and a.state ={$_GPC['state']}";

}
$data[':uniacid']=$_W['uniacid'];
$data[':cityname']=$_COOKIE["cityname"];
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;

if($type=='wait'){
  $_GPC['state']=1;
}
if(!empty($_GPC['state'])){

  $where.=" and a.state ={$_GPC['state']}";

}
$sql="SELECT a.*,b.days as typename FROM ".tablename('zhtc_yellowstore'). " a"  . " left join " . tablename("zhtc_yellowset") . " b on b.id=a.rz_type".$where. " ORDER BY a.sort ASC";
$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zhtc_yellowstore'). " a" ." left join " . tablename("zhtc_yellowset") . " b on b.id=a.rz_type".$where,$data);
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='delete'){
    $res=pdo_delete('zhtc_yellowstore',array('id'=>$_GPC['id']));
    if($res){
         message('删除成功！', $this->createWebUrl2('dlinyellowstore'), 'success');
        }else{
              message('删除失败！','','error');
        }
}
if($_GPC['op']=='tg'){
    $res=pdo_update('zhtc_yellowstore',array('state'=>2,'sh_time'=>time()),array('id'=>$_GPC['id']));
    if($res){
         message('通过成功！', $this->createWebUrl2('dlinyellowstore'), 'success');
        }else{
              message('通过失败！','','error');
        }
}
if($_GPC['op']=='jj'){
    $res=pdo_update('zhtc_yellowstore',array('state'=>3,'sh_time'=>time()),array('id'=>$_GPC['id']));
    if($res){
         message('拒绝成功！', $this->createWebUrl2('dlinyellowstore'), 'success');
        }else{
              message('拒绝失败！','','error');
        }
}


if(checksubmit('export_submit', true)) {
  if($_GPC['time']['start']==$_GPC['time']['end']){
        $dcstart=strtotime(date('Y-m-d 00:00:00'));
        $dcend=strtotime(date('Y-m-d 23:59:59'));
        $note=$_GPC['time']['start']."黄页数据";
    }else{
        $dcstart=strtotime($_GPC['time']['start']." 00:00:00");
        $dcend=strtotime($_GPC['time']['end']." 23:59:59");

        $note=$_GPC['time']['start']."至".$_GPC['time']['end']."黄页数据";
    }
  $count = pdo_fetchcolumn("SELECT COUNT(*) FROM". tablename("zhtc_yellowstore")." WHERE uniacid={$_W['uniacid']} and state=2 and sh_time>={$dcstart} and sh_time<={$dcend} and cityname='{$_COOKIE["cityname"]}' ");
  $pagesize = ceil($count/5000);
        //array_unshift( $names,  '活动名称'); 

  $header = array(
    'id'=>'帖子id',
    'logo' => 'logo图片', 
    'company_name' => '公司名称', 
    'company_address' => '公司地址', 
    'coordinates' => '公司坐标',
    'link_tel' => '联系电话', 
    'dq_time' => '到期时间', 
    'typeName' => '一级分类',
    'nTypeName' => '二级分类',
    'cityname' => '所属城市', 
    'sh_time' => '入驻时间'
    );

  $keys = array_keys($header);
  $html = "\xEF\xBB\xBF";
  foreach ($header as $li) {
    $html .= $li . "\t ,";
  }
  $html .= "\n";
for ($j = 1; $j <= $pagesize; $j++) {
    $sql = "select a.*,b.type_name as typeName,c.name as nTypeName from " . tablename("zhtc_yellowstore")."  a"  . " left join " . tablename("zhtc_yellowtype")." b on a.type_id=b.id left join " . tablename("zhtc_yellowtype2")." c on a.type2_id=c.id  WHERE a.uniacid={$_W['uniacid']} and a.state=2 and a.sh_time>={$dcstart} and a.sh_time<={$dcend} and a.cityname='{$_COOKIE["cityname"]}' limit " . ($j - 1) * 5000 . ",5000 ";
    $list = pdo_fetchall($sql);            
	if (!empty($list)) {
	    $size = ceil(count($list) / 500);
	    for ($i = 0; $i < $size; $i++) {
	      $buffer = array_slice($list, $i * 500, 500);
	      $user = array();
	      foreach ($buffer as $k =>$row) {
	     	$row['logo']=str_replace(',','#',$row['logo']);
	     	$row['coordinates']=str_replace(',','#',$row['coordinates']);
	        $row['dq_time']=date('Y-m-d H:i:s',$row['dq_time']);
	        $row['sh_time']=date('Y-m-d H:i:s',$row['sh_time']);
	        $row['cityname']=$row['cityname']?:'全国';
	        foreach ($keys as $key) {
	          $data5[] = $row[$key];
	        }
	        $user[] = implode("\t ,", $data5) . "\t ,";
	        unset($data5);
	      }
	      	$html .= implode("\n", $user) . "\n";
	    }
	}
}
  
  header("Content-type:text/csv");
  header("Content-Disposition:attachment; filename=".$note.".csv");
  echo $html;
  exit();
}
include $this->template('web/dlinyellowstore');